{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "from PIL import Image, ImageDraw\n",
    "\n",
    "import sys\n",
    "sys.path.append('..')\n",
    "from detector.input_pipeline import Pipeline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Build a graph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "params = {\n",
    "    'min_dimension': 640,\n",
    "    'batch_size': 14,\n",
    "    'image_height': 640,\n",
    "    'image_width': 640,\n",
    "}\n",
    "\n",
    "pipeline = Pipeline(\n",
    "    ['/home/dan/datasets/COCO/train_shards/shard-0000.tfrecords'],\n",
    "    is_training=True, params=params\n",
    ")\n",
    "\n",
    "dataset = pipeline.dataset\n",
    "iterator = tf.data.Iterator.from_structure(dataset.output_types, dataset.output_shapes)\n",
    "init = iterator.make_initializer(dataset)\n",
    "features, labels = iterator.get_next()\n",
    "features.update(labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Show an augmented image with boxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('coco_labels.txt', 'r') as f:\n",
    "    label_encoder = {line.strip(): i for i, line in enumerate(f.readlines()) if line.strip()}\n",
    "\n",
    "label_decoder = {i: n for n, i in label_encoder.items()}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def draw_boxes(image, boxes, labels):\n",
    "    image_copy = image.copy()\n",
    "    draw = ImageDraw.Draw(image_copy, 'RGBA')\n",
    "\n",
    "    for box, label in zip(boxes, labels):\n",
    "        ymin, xmin, ymax, xmax = box\n",
    "\n",
    "        fill = (255, 0, 0, 75)\n",
    "        outline = 'black'\n",
    "\n",
    "        draw.rectangle(\n",
    "            [(xmin, ymin), (xmax, ymax)],\n",
    "            fill=fill, outline=outline\n",
    "        )\n",
    "        draw.text((xmin + 2, ymin), label_decoder[label])\n",
    "\n",
    "    return image_copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with tf.Session() as sess:\n",
    "    sess.run(init)\n",
    "    output = sess.run(features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "i = 0\n",
    "image = Image.fromarray((255.0*output['images'][i]).astype('uint8'))\n",
    "num_boxes = output['num_boxes'][i]\n",
    "boxes = output['boxes'][i][:num_boxes].copy()\n",
    "classes = output['labels'][i][:num_boxes].copy()\n",
    "\n",
    "w, h = image.size\n",
    "assert w % 128 == 0 and h % 128 == 0\n",
    "scaler = np.array([h, w, h, w], dtype='float32')\n",
    "boxes *= scaler\n",
    "\n",
    "print(image.size)\n",
    "draw_boxes(image, boxes, classes)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
